package com.web;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Emp;
import com.entity.UserInfo;

@Controller
@RequestMapping("/user")  //namespace命名空间
@SessionAttributes(names={"e","names"},types={Integer.class})
public class UserController {
	@RequestMapping(value="/register/*/front",
			method=RequestMethod.POST,
			params={"loginId","loginPwd=123456"},
			headers={"Accept-Language=zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"})
	public String register(UserInfo userInfo){
		System.out.println(userInfo);
		return "success";
	}
	
	@RequestMapping("/save")
	public String save(@RequestHeader("Host") String hosts,@RequestHeader("Referer") String Referer){
		System.out.println("hosts==" + hosts);
		System.out.println("Referer==" + Referer);
		return "success";
	}
	
	@RequestMapping("/json")
	@ResponseBody
	public UserInfo getJSON(){
		UserInfo userInfo = new UserInfo();
		userInfo.setId(1001);
		userInfo.setLoginId("admin");
		userInfo.setLoginPwd("123456");
		return userInfo;
	}
	
	//实现重定向
	@RequestMapping("/toLogin")
	public String toLogin(){
		System.out.println("xxxxxxxxxxxxxxxxxx");
		return "redirect:/login.jsp";
	}
	
	//存储模型数据到页面
	@RequestMapping(value="/saveEmp4")
	public String saveEmpToPage4(Map<String, Object> map,Emp emp){
		System.out.println(emp);
		map.put("e", emp);
		map.put("names", Arrays.asList("张无忌","赵敏"));
		map.put("age", 35);
		return "success";
	}

	
	//存储模型数据到页面
	@RequestMapping(value="/saveEmp3")
	public String saveEmpToPage2(Map<String, Emp> map,Emp emp){
		map.put("emp", emp);
		return "success";
	}
	
	//存储模型数据到页面
	@RequestMapping(value="/saveEmp2")
	public ModelAndView saveEmpToPage(Emp emp){
		ModelAndView mav = new ModelAndView("success");
		mav.addObject("emp", emp);
		return mav;
	}
	
	
	@RequestMapping(value="/writer")
	public void testWriter(Writer writer) throws IOException{		
		writer.write("你好SpingMVC");
	}
	
	@RequestMapping(value="/servlet")
	public String testServlet(HttpServletRequest request,
			HttpServletResponse response,
			HttpSession session){
		System.out.println(request);
		System.out.println(response);
		System.out.println(session);
		return "success";
	}
	
	
	@RequestMapping(value="/saveEmp")
	public String saveEmp(Emp emp){
		System.out.println(emp);
		return "success";
	}
	
	
	
	@RequestMapping(value="/saveUser")
	public String saveUser(@RequestParam(name="username") String username,
			@RequestParam(value="age") int age){
		System.out.println("获取请求参数: username="+username+",age="+age);
		return "success";
	}
	
	
	@RequestMapping(value="/update",method=RequestMethod.PUT)
	public String putUser(){
		System.out.println("执行putUser方法.....");
		return "success";
	}
	
	
	@RequestMapping("/delete/{id}/{age}")
	public String deleteUser(@PathVariable("id") Integer id1,
			                 @PathVariable Integer age){
		System.out.println("删除用户: id="+id1+",age="+age);
		return "success";
	}
	
	@RequestMapping(value="/*/search")
	public String search(){
		System.out.println("执行Search方法........");
		return "success";
	}
	
	
	@RequestMapping(value="/get",
			        method=RequestMethod.POST,
			        params={"username","age=30"} )
	public String getUser(){
		System.out.println("get方法获取用户信息.......");
		return "success";
	}
	
	
	/**
	 * 1.配置多个Action,一个Action对应一个方法 execute
	 * 2.动态方法调用 userAction!save.action
	 * 3.通配符userAction_*.action  method="{1}"
	 * @return
	 */
	
	@RequestMapping("/save")
	public String saveUser(){
		System.out.println("添加用户信息.......");
		
		///WEB-INF/views/+ success + ".jsp"
		return "success";
	}
}
